Title: SQL W3S (By-BKK) / 033 Group By
Tags: Obsidian_to_Anki

Q: What does the SQL GROUP BY statement do?
A: It groups rows that have the same values into summary rows

Q: Which SQL clause creates result sets like “number of customers in each country”?
A: GROUP BY statement

Q: GROUP BY is usually used with which functions?
A: Aggregate functions: COUNT(), MAX(), MIN(), SUM(), AVG()

Q: What is the syntax for using GROUP BY in SQL?
A: SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s);

Q: True/False: GROUP BY can group by more than one column
A: True

Q: How do you count the number of customers from each country in a table named Customers?
A: SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;

Q: How do you sort the grouped result by the number of customers in descending order?
A: SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER BY COUNT(CustomerID) DESC;

Q: What SQL clause do you use with GROUP BY to sort results?
A: ORDER BY

Q: GROUP BY can be used with JOINs. True or False?
A: True

Q: How do you list the number of orders sent by each shipper using JOIN and GROUP BY?
A: SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID GROUP BY ShipperName;

Q: Which aggregate functions are commonly used with GROUP BY?
A: COUNT(), MAX(), MIN(), SUM(), AVG()

Q: COUNT(), MAX(), MIN(), SUM(), AVG() are used mostly with which SQL clause?
A: GROUP BY

Q: Give an example of a GROUP BY query using aggregate functions
A: SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;

Q: Reverse card:
Groups rows with common column values into summary rows
A: What does the GROUP BY statement in SQL do?

Q: Reverse card:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
A: How do you find the number of customers for each country?

Q: Reverse card:
ORDER BY clause
A: Which clause is used to sort results after GROUP BY?

Q: True/False: GROUP BY must be used with aggregate functions
A: False

Q: GROUP BY statement
A: Which SQL statement is best suited for summarizing rows such as totals or counts?

Q: Reverse card:
Can GROUP BY be used with JOINs?
A: Yes